home *** CD-ROM | disk | FTP | other *** search
- /*
- * pup.c
- *
- * Copyright © 1990 Michael S. Engber. All rights reserved
- */
-
- #include "pup.h"
-
- #define INDENT 5
- #define XTRA_INDENT 9
-
-
- static Rect PupMenuRect(ControlHandle theControl, Str255 curItem){
- /*
- * Returns the pop-up box for curItem & trims curItem if needed.
- */
- MenuHandle menuH = (MenuHandle)(*theControl)->contrlData;
- Rect r = (*theControl)->contrlRect;
- Point p = *(Point*)(&(*theControl)->contrlRfCon);
- int maxWid;
-
- GetItem(menuH,(*theControl)->contrlValue,curItem);
-
- r.left = p.h;
- r.bottom = p.v;
-
- maxWid = r.right - r.left - 2*INDENT - XTRA_INDENT - 1;
-
- while(curItem[0]>1 && StringWidth(curItem)>maxWid){
- --curItem[0];
- curItem[curItem[0]] = '…';
- }
-
- r.right = r.left + StringWidth(curItem) + 2*INDENT + XTRA_INDENT;
-
- OffsetRect(&r,0,1);
- return r;
- }
-
-
- static Rect PupTitleRect(ControlHandle theControl){
- MenuHandle menuH = (MenuHandle)(*theControl)->contrlData;
- Rect r = (*theControl)->contrlRect;
- Point p = *(Point*)(&(*theControl)->contrlRfCon);
-
- r.right = p.h;
- r.bottom = p.v;
- r.left = r.right - 2*INDENT - StringWidth((*menuH)->menuData);
-
- OffsetRect(&r,0,1);
- return r;
- }
-
- /*----------------------------------------------------------------------*/
-
- static long DrawCntl(ControlHandle theControl, long param){
- MenuHandle menuH = (MenuHandle)(*theControl)->contrlData;
- Rect menuRect;
- Str255 curItem;
- FontInfo fInfo;
-
- if(!(*theControl)->contrlVis) return 0L;
-
- menuRect = PupMenuRect(theControl,curItem);
- GetFontInfo(&fInfo);
-
- EraseRect(&(*theControl)->contrlRect);
-
- MoveTo(menuRect.left - INDENT - StringWidth((*menuH)->menuData),
- menuRect.top + fInfo.ascent);
- DrawString((*menuH)->menuData);
-
- MoveTo(menuRect.left + INDENT + XTRA_INDENT,
- menuRect.top + fInfo.ascent);
-
- DrawString(curItem);
-
-
- InsetRect(&menuRect,-1,-1);
- FrameRect(&menuRect);
- MoveTo(menuRect.left + 2,menuRect.bottom);
- LineTo(menuRect.right,menuRect.bottom);
- LineTo(menuRect.right,menuRect.top + 2);
-
- return 0L;
- }
-
- static long TestCntl(ControlHandle theControl, long param){
- Rect menuRect;
- Str255 curItem;
-
- menuRect = PupMenuRect(theControl,curItem);
- if ((*theControl)->contrlHilite!=255 && PtInRect(param,&menuRect))
- return (long)inThumb;
- else
- return 0L;
- }
-
- static long CalcCRgns(ControlHandle theControl, long param){
- RectRgn((RgnHandle)param,&(*theControl)->contrlRect);
- return 0L;
- }
-
- static long InitCntl(ControlHandle theControl, long param){
- FontInfo fInfo;
- Point p;
-
- /* first try to use contrlMin as the rsrc id of a MENU */
- if((*theControl)->contrlMin > 0)
- (*theControl)->contrlData = (Handle)GetMenu((*theControl)->contrlMin);
- else{
- (*theControl)->contrlMin = Unique1ID('MENU');
- (*theControl)->contrlData = (Handle)0L;
- }
-
- /* if above fails, create a menu with id = cntrlMin */
- if(!(*theControl)->contrlData)
- (*theControl)->contrlData = (Handle)NewMenu((*theControl)->contrlMin,
- (*theControl)->contrlTitle);
-
- /* bottom right point of pop-up title box */
- GetFontInfo(&fInfo);
- p.h = (*theControl)->contrlRect.left + (*theControl)->contrlMax;
- p.v = (*theControl)->contrlRect.top + fInfo.ascent + fInfo.descent + fInfo.leading;
-
- /* Set Min & Max - set Max really large in case items are added later */
- (*theControl)->contrlMin = 1;
- (*theControl)->contrlMax = 9999;
-
- /* contrlRfCon holds ResType the menu lists - 0 if none */
- if((*theControl)->contrlRfCon)
- AddResMenu((MenuHandle)(*theControl)->contrlData,
- (ResType)(*theControl)->contrlRfCon);
-
- /* store bottom right point of pop-up title box in contrlRfCon */
- *(Point*)(&(*theControl)->contrlRfCon) = p;
-
-
- return 0L;
- }
-
- static long DispCntl(ControlHandle theControl, long param){
- MenuHandle menuH = (MenuHandle)(*theControl)->contrlData;
-
- if(HomeResFile(menuH) > 0){
- ReleaseResource(menuH);
- }else{
- DisposeMenu(menuH);
- }
-
- return 0L;
- }
-
- static long PosCntl(ControlHandle theControl, long param){
- return 0L;
- }
-
- static long ThumbCntl(ControlHandle theControl, long param){
- return 0L;
- }
-
- static long DragCntl(ControlHandle theControl, long param){
- MenuHandle menuH = (MenuHandle)(*theControl)->contrlData;
- Rect menuRect;
- Rect titleRect = PupTitleRect(theControl);
- Str255 curItem;
- short oldItem;
- short newItem;
-
- Point popPt;
-
- if(param == 0L) return 0L;
-
- InvertRect(&titleRect);
-
- menuRect = PupMenuRect(theControl,curItem);
- oldItem = (*theControl)->contrlValue;
-
- CheckItem(menuH,oldItem,(char)1);
-
- popPt.v = menuRect.top;
- popPt.h = menuRect.left;
- LocalToGlobal(&popPt);
-
- InsertMenu(menuH,-1);
- newItem = LoWord(PopUpMenuSelect(menuH,popPt.v,popPt.h ,oldItem));
- DeleteMenu((*menuH)->menuID);
-
- if(newItem > 0) (*theControl)->contrlValue = newItem;
-
-
- CheckItem(menuH,oldItem,(char)0);
-
- DrawCntl(theControl,0L);
-
- return 1L;
- }
-
- static long AutoTrack(ControlHandle theControl, long param){
- return 0L;
- }
-
- /*----------------------------------------------------------------------*/
-
- pascal long mainCDEF(int varCode, ControlHandle theControl, int message, long param){
- long result;
- SignedByte hState;
- GrafPtr gPort;
- PenState pState;
- short tSize;
- short tFont;
- Style tFace;
- short tMode;
- RgnHandle clipRgn;
- RgnHandle updateRgn;
-
- hState = HGetState(theControl);
- HLock(theControl);
-
-
- clipRgn = NewRgn();
- GetClip(clipRgn);
- GetPort(&gPort);
- GetPenState(&pState);
- tSize = gPort->txSize;
- tFont = gPort->txFont;
- tFace = gPort->txFace;
- tMode = gPort->txMode;
-
-
- updateRgn = NewRgn();
- RectRgn(updateRgn,&(*theControl)->contrlRect);
- SectRgn(updateRgn,clipRgn,updateRgn);
- SetClip(updateRgn);
- DisposeRgn(updateRgn);
-
- PenNormal();
- TextSize(12);
- TextFont(systemFont);
- TextFace(0);
- TextMode(srcCopy);
-
-
- switch(message){
- case drawCntl: result = DrawCntl(theControl,param); break;
- case testCntl: result = TestCntl(theControl,param); break;
- case calcCRgns: result = CalcCRgns(theControl,param); break;
- case initCntl: result = InitCntl(theControl,param); break;
- case dispCntl: result = DispCntl(theControl,param); break;
- case posCntl: result = PosCntl(theControl,param); break;
- case thumbCntl: result = ThumbCntl(theControl,param); break;
- case dragCntl: result = DragCntl(theControl,param); break;
- case autoTrack: result = AutoTrack(theControl,param); break;
- }
-
-
- SetPenState(&pState);
- TextSize(tSize);
- TextFont(tFont);
- TextFace(tFace);
- TextMode(tMode);
-
- SetClip(clipRgn);
- DisposeRgn(clipRgn);
-
-
- HSetState(theControl,hState);
- return result;
- }